package com.lh.ssm.service.impl;

import com.lh.ssm.domain.Address;
import com.lh.ssm.mapper.AddressMapper;
import com.lh.ssm.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
@Service
public class AddressServiceImpl implements AddressService{

    @Autowired
    private AddressMapper addressMapper;
    @Override
    public List<Address> findAddressByUid(int uid) {

        return addressMapper.selectAddressByUid(uid);
    }

    @Override
    public int addByUser(String uid, String name, String phone, String detailAddress) {
        return addressMapper.addAddressByUid(uid,name,phone,detailAddress);
    }

    @Override
    public int setAddress(int uid, String aid) {
        //实务操作：保证同时成功或者失败

        //将非aid地址设置为非默认地址
        addressMapper.updateAddressToCommon(aid, uid);

        //将aid这个地址设置为默认地址
        addressMapper.updateAddressToDefault(aid);
        return 0;
    }

    @Override
    public int updateAddress(Address address) {
        return addressMapper.updateAddressById(address);
    }

    @Override
    public int deleteAddressById(String aid) {
        return addressMapper.deleteAddressById(aid);
    }
}
